package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.BTeamInfoMapper;
import com.tulele.vtool.mapper.BTeamRelationMapper;
import com.tulele.vtool.model.BTeamInfo;
import com.tulele.vtool.model.BTeamRelation;
import com.tulele.vtool.service.BTeamInfoService;
import com.tulele.vtool.utils.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 团队基础信息表(BTeamInfo)表服务实现类
 *
 * @author makejava
 * @since 2020-09-09 16:08:52
 */
@Service("bTeamInfoService")
public class BTeamInfoServiceImpl implements BTeamInfoService {
    /**
     * 服务对象
     */
    @Resource
    private BTeamInfoMapper bTeamInfoMapper;

    /**
     * 服务对象
     */
    @Resource
    private BTeamRelationMapper bTeamRelationMapper;

    /**
     * 服务对象
     */
//    @Value("${spring.profiles.active}")
//    private String springProfilesActive;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BTeamInfo queryById(Integer id) {
        return this.bTeamInfoMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<BTeamInfo> queryAllByLimit(int offset, int limit) {
        return this.bTeamInfoMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param bTeamInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BTeamInfo insert(BTeamInfo bTeamInfo) {
        this.bTeamInfoMapper.insert(bTeamInfo);
        return bTeamInfo;
    }

    /**
     * 修改数据
     *
     * @param bTeamInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BTeamInfo update(BTeamInfo bTeamInfo) {
        this.bTeamInfoMapper.update(bTeamInfo);
        return this.queryById(bTeamInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bTeamInfoMapper.deleteById(id) > 0;
    }

    /**
     * 新增数据
     *
     * @param bTeamInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BTeamInfo addOneBTeamInfByUserId(BTeamInfo bTeamInfo,String springProfilesActive) {
        bTeamInfo.setTeamName(bTeamInfo.getTeamName());//团队名称
        bTeamInfo.setUserId(bTeamInfo.getUserId());//创建人userId
        bTeamInfo.setTeamFlag(1);//展示状态（0停用 1正常）
        bTeamInfo.setGzhCode(springProfilesActive);//公众号标识
        bTeamInfo.setShareNum(0);//团队分享次数
        bTeamInfo.setCreateTime(DateUtil.getCurDate());//创建时间
        bTeamInfo.setUpdateTime(DateUtil.getCurDate());//更新时间
        bTeamInfo.setTeamRemark("");//备注

        this.bTeamInfoMapper.insert(bTeamInfo);

        BTeamRelation bTeamRelationDto = new BTeamRelation();

        bTeamRelationDto.setUserId(bTeamInfo.getUserId());//成员userId
        bTeamRelationDto.setGzhCode(springProfilesActive);//公众号标识
        bTeamRelationDto.setUserTeamNow(1);//当前所属团队 0-离开 1-正在

        List<BTeamRelation> bTeamRelationList = this.bTeamRelationMapper.queryAll(bTeamRelationDto);

        //如果有团队先离开
        if (CollectionUtils.isNotEmpty(bTeamRelationList)) {
            for (int i = 0; i < bTeamRelationList.size(); i++) {
                BTeamRelation bTeamRelationOne = new BTeamRelation();
                BTeamRelation upset = bTeamRelationList.get(i);

                bTeamRelationOne.setId(upset.getId());
                bTeamRelationOne.setUserTeamNow(0);//当前所属团队 0-离开 1-正在
                this.bTeamRelationMapper.update(bTeamRelationOne);
            }
        }

        bTeamRelationDto.setTeamId(bTeamInfo.getId());//团队ID
        bTeamRelationDto.setTeamName(bTeamInfo.getTeamName());//团队名称
        bTeamRelationDto.setUserId(bTeamInfo.getUserId());//成员userId
        bTeamRelationDto.setGzhCode(springProfilesActive);//公众号标识
        bTeamRelationDto.setUserTeamNow(1);//当前所属团队 0-离开 1-正在
        bTeamRelationDto.setInviterId(null);//邀请人userId
        bTeamRelationDto.setTeamRole(2);//成员角色 0-团队普通成员 1-团队管理员 2-团队队长
        bTeamRelationDto.setUserFlag(1);//展示状态（0停用 1正常）
        bTeamRelationDto.setCreateTime(DateUtil.getCurDate());//创建时间
        bTeamRelationDto.setUpdateTime(DateUtil.getCurDate());//更新时间
        bTeamRelationDto.setTeamRemark("");//备注

        this.bTeamRelationMapper.insert(bTeamRelationDto);

        return bTeamInfo;
    }

    /**
     * 通过BTeamInfo查询单条数据
     *
     * @param bTeamInfo
     * @return 实例对象
     */
    @Override
    public BTeamInfo queryOne(BTeamInfo bTeamInfo){
        return this.bTeamInfoMapper.queryOne(bTeamInfo);
    }
}
